// 对应困难帮扶台账的API
import { postRequest, getRequest, importRequestData, postBodyRequest, bodyPostRequest } from '@/libs/axios';

const getLocalData = (url, params) => {
    let data = sessionStorage.getItem(url + JSON.stringify(params));
    if (data) {
        return Promise.resolve(JSON.parse(data));
    } else {
        return Promise.reject();
    }
};

async function getReqAndLocal(url, params) {
    try {
        let result = await getLocalData(url, params);
        new Promise(() => {
            getRequest(url, params).then(res => {
                sessionStorage.setItem(url + JSON.stringify(params), JSON.stringify(res));
            });
        });
        return Promise.resolve(result);
    } catch {
        // 从本地获取失败
        let r = await getRequest(url, params);
        sessionStorage.setItem(url + JSON.stringify(params), JSON.stringify(r));
        console.log('本地无数据，从网络获取');
        return Promise.resolve(r);
    }
}

async function postReqAndLocal(url, params) {
    try {
        let result = await getLocalData(url, params);
        new Promise(() => {
            postRequest(url, params).then(res => {
                console.log('更新成功');
                sessionStorage.setItem(url + JSON.stringify(params), JSON.stringify(res));
            });
        });
        console.log('已经从本地拿到数据了');
        return Promise.resolve(result);
    } catch {
        // 从本地获取失败
        let r = await postRequest(url, params);
        sessionStorage.setItem(url + JSON.stringify(params), JSON.stringify(r));
        console.log('本地无数据，从网络获取');
        return Promise.resolve(r);
    }
}

//
export const getJyqx = params => {
    return postRequest('/HardshipSupport/getJyqx', params);
};

// 条件查询列表
export const getList = params => {
    return postRequest('/HardshipSupport/getByCondition', params);
};

export const truncateData = params => {
    return postRequest('/HardshipSupport/truncateData', params);
};

// 按照身份证号查找详细信息
export const getDetailByIdCard = params => {
    return postRequest('/HardshipSupport/getByIdCard', params);
};

// 按照type信息查询映射关系
export const getNoSourceMessage = params => {
    return getRequest('/HardshipSupport/getNoSourceMessage',params);
};

// 按照type信息查询映射关系
export const getMapByType = type => {
    return getReqAndLocal('/dictData/getByType/' + type, {});
};
// 拿到院系代号
export const getDeptCode = () => {
    return postReqAndLocal('/HardshipSupport/getCollege', {});
};

// 就业类别
// { dictMatch:0, version: date } const version = new Date().getFullYear()
export const getEmploymentCategory = params => postReqAndLocal('/gqQxCode/getTree', params);

// 添加帮扶对象
export const addHardshipSupport = params => {
    return postRequest('/HardshipSupport/add', params, {
        transformRequest: [
            function (data) {
                let ret = '';
                for (let it in data) {
                    // 如果是数组 则 item[0].xxx = xxx item[1].xxx = xxx
                    // 如果是对象 则 item.xxx = xxx
                    // 如果是字符串 则 item = xxx
                    if (Array.isArray(data[it])) {
                        for (let i = 0; i < data[it].length; i++) {
                            for (let item in data[it][i]) {
                                ret += encodeURIComponent(it) + '[' + i + '].' + encodeURIComponent(item) + '=' + encodeURIComponent(data[it][i][item]) + '&';
                            }
                        }
                    } else if (typeof data[it] === 'object') {
                        for (let item in data[it]) {
                            ret += encodeURIComponent(it) + '.' + encodeURIComponent(item) + '=' + encodeURIComponent(data[it][item]) + '&';
                        }
                    } else {
                        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
                    }
                }
                ret = ret.substring(0, ret.length - 1);
                return ret;
            }
        ]
    });
};

// 根据层级和学历获取专业级联菜单
export const getMajor = params => {
    return postReqAndLocal('/professionalCode/getByXlAndLevel', params);
};

// 通过id查看台账详细信息 注意 这不是身份证
export const getHardshipSupportById = params => {
    return getRequest('/HardshipSupport/getHardDetails', params);
};

export const editDataCheck = params => {
    return postRequest('/HardshipSupport/editDataCheck', params);
};
// getSelectMap
// 好像是因为 如果是省级登录 展示页面不需要院系代码
export const getSelectMap = weatherGetCollege => {
    if (weatherGetCollege) {
        return Promise.all([getMapByType('sex'), getMapByType('ethnic'), getMapByType('eduLevelType'), getMapByType('political'), getDeptCode(), getMapByType('qx')]);
    } else {
        return Promise.all([getMapByType('sex'), getMapByType('ethnic'), getMapByType('eduLevelType'), getMapByType('political'), Promise.resolve(), getMapByType('qx')]);
    }
};

// 获取全部的专业 三层
export const getAllProfession = gradLevelDict => {
    return Promise.all([
        getMajor({
            gradLevelDict,
            level: 0
        }),
        getMajor({
            gradLevelDict,
            level: 1
        }),
        getMajor({
            gradLevelDict,
            level: 2
        })
    ]);
};

export const importExcel = params => {
    return importRequestData('/HardshipSupport/mqImport', params);
};

export const getProfessionNameByProfCode = params => {
    return postRequest('/professionalCode/search', {
        title: params,
        version: ''
    });
};

// 修改帮扶对象 基本信息
export const updateHardshipSupport = params => {
    return bodyPostRequest('/HardshipSupport/editBasic', params);
};

// 删除谈心谈话
export const deleteHeartTalk = params => {
    return postRequest('/HardshipSupport/delHeart', params);
};

// 修改谈心谈话
export const editHeartTalk = params => {
    return postRequest('/HardshipSupport/editHeart', params);
};

// 添加谈心谈话
export const addHeartTalk = params => {
    return postRequest('/HardshipSupport/addHeart', params);
};

// 增删改 推荐 addRecommend deleteRecommend editRecommend
export const addRecommend = params => {
    return postRequest('/HardshipSupport/addRecommend', params);
};
export const deleteRecommend = params => {
    return postRequest('/HardshipSupport/delRecommend', params);
};
export const editRecommend = params => {
    return postRequest('/HardshipSupport/editRecommend', params);
};
// 增删改就业促进
export const addEmploy = params => {
    return postRequest('/HardshipSupport/addEmploy', params);
};
export const deleteEmploy = params => {
    return postRequest('/HardshipSupport/delEmploy', params);
};
export const editEmploy = params => {
    return postRequest('/HardshipSupport/editEmploy', params);
};

export const addOther = params => {
    return postRequest('/HardshipSupport/addOther', params);
};
export const editOther = params => {
    return postRequest('/HardshipSupport/editOther', params);
};

export const deleteOther = params => {
    return postRequest('/HardshipSupport/delOther', params);
};
